package com.base.smart.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.base.smart.demo.entity.UserEntity;
import com.base.smart.demo.service.UserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Log4j2
@RequestMapping("/user/")
public class UserController {


    private UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     *  测试登录，浏览器访问：http://localhost:8888/user/doLogin?username=zhang&password=123456
     * @param username 用户名
     * @param password 密码
     * @return 登录状态
     */
    @RequestMapping("doLogin")
    public String doLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001);
            return "登录成功";
        }
        return "登录失败";
    }

    /**
     * 查询登录状态，浏览器访问： http://localhost:8888/user/isLogin
     * @return 判断是否登录
     */
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    /**
     * http://localhost:8888/user/list
     * @return 用户信息
     * @throws InterruptedException
     */
    @RequestMapping("list")
    public List<UserEntity> listUsers() throws InterruptedException {

        return userService.listUsers();

    }
}
